package com.ip.student.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ip.common.constant.Result;
import com.ip.common.enums.CodeEnum;
import com.ip.common.utils.StrUtils;
import com.ip.user.models.common.annotation.RequiresPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ip.student.models.service.IScoreCourseService;
import com.ip.student.models.bo.ScoreCourseSaveBO;
import com.ip.student.models.bo.ScoreCourseBO;
import com.ip.student.models.dto.ScoreCourseDTO;

/**
 * 课程信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-06 10:15:15
 */

@RestController
@RequestMapping("/cms/scoreCourse")
@Slf4j
@Api(value = "ScoreCourseController",tags = "课程信息接口")
public class ScoreCourseController {

    @DubboReference
    private IScoreCourseService scoreCourseService;

    @ApiOperation(value = "添加课程信息",notes = "添加课程信息")
    @PostMapping(value = "/insert")
    @RequiresPermission("score:course:add")
    public Result insert(@RequestBody ScoreCourseSaveBO saveBO) {
        boolean flag = scoreCourseService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存课程信息",notes = "保存课程信息")
    @RequiresPermission("score:course:add")
    public Result save(@RequestBody ScoreCourseSaveBO saveBO){
        if(saveBO.getCourseId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改课程信息",notes = "修改课程信息")
    @PostMapping("/update")
    @RequiresPermission("score:course:update")
    public Result update(@RequestBody ScoreCourseSaveBO saveBO){
        ScoreCourseDTO dto = new ScoreCourseDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setCourseId(saveBO.getCourseId());
        boolean flag = scoreCourseService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除课程信息",notes = "删除课程信息")
    @ApiImplicitParam(name = "id",value = "课程信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    @RequiresPermission("score:course:delete")
    public Result delete(@RequestBody Map<String, Object> params){
        // 检查参数是否为空
        if (params == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        try {
            // 首先尝试从"id"键获取ID
            Object idObj = params.get("id");
            if (idObj == null) {
                // 如果"id"键不存在，尝试从"courseId"键获取ID
                idObj = params.get("courseId");
            }
            
            // 检查ID是否为空
            if (idObj == null || idObj.toString().isEmpty()) {
                return new Result<>(CodeEnum.FAIL_100002);
            }
            
            Long id = Long.valueOf(idObj.toString());
            // 检查转换后的ID是否有效
            if (id <= 0) {
                return new Result<>(CodeEnum.FAIL_100002);
            }
            
            boolean flag = scoreCourseService.deleteById(id.intValue());
            if (flag){
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL_100009);
            }
        } catch (NumberFormatException e) {
            return new Result<>(CodeEnum.FAIL_100002);
        } catch (Exception e) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }
    
    @ApiOperation(value = "根据对象删除课程信息", notes = "根据对象删除课程信息")
    @PostMapping("/deleteByObject")
    @RequiresPermission("score:course:delete")
    public Result deleteByObject(@RequestBody ScoreCourseDTO scoreCourseDTO) {
        // 检查参数是否为空
        if (scoreCourseDTO == null || scoreCourseDTO.getCourseId() == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        try {
            Long id = scoreCourseDTO.getCourseId().longValue();
            // 检查ID是否有效
            if (id <= 0) {
                return new Result<>(CodeEnum.FAIL_100002);
            }
            
            boolean flag = scoreCourseService.deleteById(id.intValue());
            if (flag){
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL_100009);
            }
        } catch (Exception e) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除课程信息",notes = "批量删除课程信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    @RequiresPermission("score:course:delete")
    public Result deleteBatch(@RequestParam("ids") String courseIds){
        if(StrUtils.isEmpty(courseIds)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = courseIds.split(",");
        scoreCourseService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    @RequiresPermission("score:course:query")
    public Result<ScoreCourseDTO> get(@RequestParam("id") Long courseId){
        if(courseId == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ScoreCourseDTO dto = scoreCourseService.selectById(courseId);

        return new Result<>(dto);
    }

    @ApiOperation(value = "课程信息列表",notes = "课程信息列表")
    @PostMapping("/queryPageList")
    @RequiresPermission("score:course:query")
    public Result<Page<ScoreCourseDTO>> queryPageList(@RequestBody ScoreCourseBO bo){
        Wrapper<ScoreCourseDTO> wrapper = new EntityWrapper<>();

        // 根据适用年级进行模糊查询
        if (StrUtils.isNotEmpty(bo.getApplyGrade())) {
            wrapper.like("apply_grade", bo.getApplyGrade());
        }
        //根据是否教育局设置课程进行模糊查询
        if (bo.getIsEduSet() != null) {
            wrapper.eq("is_edu_set", bo.getIsEduSet());
        }
        // 根据课程名称进行模糊查询
        if (StrUtils.isNotEmpty(bo.getCourseName())) {
            wrapper.like("course_name", bo.getCourseName());
        }
        //根据课程Id进行模糊查询
        if (bo.getCourseId() != null) {
            wrapper.eq("course_id", bo.getCourseId());
        }
        Page<ScoreCourseDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = scoreCourseService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}